/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.IndexManager;

import BLL.SavingType_BLL;
import BLL.Unity;
import DTO.SavingType_DTO;
import javax.swing.JOptionPane;

/**
 *
 * @author Mr.Sam
 */
public class SavingTypeManager extends javax.swing.JInternalFrame {

    /**
     * Creates new form SavingBook
     */
    SavingType_BLL savingtype_bll=new SavingType_BLL();
    TableSavingType model=new TableSavingType();
    Unity unity=new Unity();
    String sql="select TypeID,TypeName,Term,InterestRate from savingtype";
    public SavingTypeManager() throws Exception {
        initComponents();
        MaLoaiTietKiem.setEditable(false);
        model.SetTable(Table);
        model.SetData(sql);
        LoaiSo.addItem("Khong Ky Han");
        LoaiSo.addItem("Co Ky Han");
    }
    public void ReloadTable(String sql) throws Exception
    {
        model.RemoveData();
        model.SetData(sql);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        KyHan = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        LaiXuat = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        Table = new org.jdesktop.swingx.JXTable();
        ThemMoi = new javax.swing.JButton();
        ThemLoaiTietKiem = new javax.swing.JButton();
        XoaLoaiTietKiem = new javax.swing.JButton();
        CapNhatLoaiTietKiem = new javax.swing.JButton();
        MaLoaiTietKiem = new javax.swing.JTextField();
        LoaiSo = new javax.swing.JComboBox();

        setTitle("Quản Lý Loại Tiết Kiệm");

        jLabel1.setText("Loại Sổ");

        jLabel2.setText("Kỳ Hạn");

        jLabel3.setText("Mã Loại Tiết Kiệm");

        jLabel4.setText("Lãi Xuất");

        Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        Table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(Table);

        ThemMoi.setText("Thêm Mới");
        ThemMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ThemMoiActionPerformed(evt);
            }
        });

        ThemLoaiTietKiem.setText("Thêm Loại Tiết Kiệm");
        ThemLoaiTietKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ThemLoaiTietKiemActionPerformed(evt);
            }
        });

        XoaLoaiTietKiem.setText("Xóa");
        XoaLoaiTietKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                XoaLoaiTietKiemActionPerformed(evt);
            }
        });

        CapNhatLoaiTietKiem.setText("Cập Nhật Loại Tiết Kiệm");
        CapNhatLoaiTietKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CapNhatLoaiTietKiemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 982, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(LoaiSo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(MaLoaiTietKiem, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
                .addGap(85, 85, 85)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(27, 27, 27)
                        .addComponent(LaiXuat, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)
                        .addComponent(ThemMoi)
                        .addGap(32, 32, 32)
                        .addComponent(ThemLoaiTietKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(27, 27, 27)
                        .addComponent(KyHan, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(53, 53, 53)
                        .addComponent(XoaLoaiTietKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(CapNhatLoaiTietKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MaLoaiTietKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LaiXuat, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ThemMoi, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ThemLoaiTietKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(LoaiSo, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(KyHan, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(XoaLoaiTietKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CapNhatLoaiTietKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(54, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TableMouseClicked
        // TODO add your handling code here:
        int index=Table.getSelectedRow();
        MaLoaiTietKiem.setText(Table.getValueAt(index, 0)+"");
        LoaiSo.setSelectedItem(Table.getValueAt(index, 1)+"");
        KyHan.setText(Table.getValueAt(index, 2)+"");
        LaiXuat.setText(Table.getValueAt(index, 3)+"");
        
    }//GEN-LAST:event_TableMouseClicked

    private void ThemMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ThemMoiActionPerformed
        // TODO add your handling code here:
        MaLoaiTietKiem.setText("");
        //LoaiSo.setSelectedItem();
        KyHan.setText("");
        LaiXuat.setText("");
    }//GEN-LAST:event_ThemMoiActionPerformed

    private void ThemLoaiTietKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ThemLoaiTietKiemActionPerformed
        // TODO add your handling code here:
        try {
         
            savingtype_bll.Insert("{call Insert_SavingType(?,?,?)}", GetSavingTypeDTO());
            JOptionPane.showMessageDialog(null, "Thêm Thành Công???");
            ReloadTable(sql);
        } catch (Exception e) {
        }
    }//GEN-LAST:event_ThemLoaiTietKiemActionPerformed

    private void XoaLoaiTietKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_XoaLoaiTietKiemActionPerformed
        // TODO add your handling code here:
        try {
            savingtype_bll.Delete("{call Delete_SavingType(?)}",GetSavingTypeDTO());
            JOptionPane.showMessageDialog(null, "Xóa Thành Công???");
            ReloadTable(sql);
        } catch (Exception e) {
        }
    }//GEN-LAST:event_XoaLoaiTietKiemActionPerformed
    public SavingType_DTO GetSavingTypeDTO()
    {
            SavingType_DTO info=new SavingType_DTO();
            info.setTypeID(MaLoaiTietKiem.getText());
            info.setName(LoaiSo.getSelectedItem().toString());
            info.setTerm(Integer.parseInt(KyHan.getText()));
            info.setInterestRate(Float.parseFloat(LaiXuat.getText()));
            return info;
    }
    private void CapNhatLoaiTietKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CapNhatLoaiTietKiemActionPerformed
        // TODO add your handling code here:
        try {
            
            
            savingtype_bll.Update("{call Update_SavingType(?,?,?,?)}", GetSavingTypeDTO());
            JOptionPane.showMessageDialog(null, "Cập Nhật Thành Công???");
            ReloadTable(sql);
        } catch (Exception e) {
        }
    }//GEN-LAST:event_CapNhatLoaiTietKiemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CapNhatLoaiTietKiem;
    private javax.swing.JTextField KyHan;
    private javax.swing.JTextField LaiXuat;
    private javax.swing.JComboBox LoaiSo;
    private javax.swing.JTextField MaLoaiTietKiem;
    private org.jdesktop.swingx.JXTable Table;
    private javax.swing.JButton ThemLoaiTietKiem;
    private javax.swing.JButton ThemMoi;
    private javax.swing.JButton XoaLoaiTietKiem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
